package contract;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import code.CodeDAO;
import code.CodeDTO;

public class ContractView extends HttpServlet{
	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		doPost(req, resp);
	}
	
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		String ct_no = req.getParameter("ct_no");
		//System.out.println(ct_no);
		ContractDTO cont = new ContractDTO();
		cont.setCt_no(ct_no);
		ContractDAO dao = new ContractDAO();
		dao.setCont(cont);
		ContractDTO dto = dao.view();
		req.setAttribute("dto", dto);
		//공통코드 관련
		CodeDTO code = new CodeDTO();
		CodeDAO cdao = new CodeDAO();
		//계약상태
		code.setCd_grp1("CTING");
		cdao.setDto(code);
		ArrayList<CodeDTO> codes = cdao.returnCode();
		for(int i=0; i<codes.size();i++){
			CodeDTO cd = codes.get(i);
			if(dto.getCt_ing().equals(cd.getCd_grp2())){
				req.setAttribute("cting", cd.getCd_grp2_nm());
				break;
			}
		}
		//직급
		cdao = new CodeDAO();
		code.setCd_grp1("PST");
		cdao.setDto(code);
		codes = cdao.returnCode();
		for(int i=0; i<codes.size();i++){
			CodeDTO cd = codes.get(i);
			if(dto.getSa_gd().equals(cd.getCd_grp2())){
				req.setAttribute("pst", cd.getCd_grp2_nm());
				break;
			}
		}
		//부서
		cdao = new CodeDAO();
		code.setCd_grp1("SADP");
		cdao.setDto(code);
		codes = cdao.returnCode();
		for(int i=0; i<codes.size();i++){
			CodeDTO cd = codes.get(i);
			if(dto.getSa_dpm().equals(cd.getCd_grp2())){
				req.setAttribute("sadp", cd.getCd_grp2_nm());
				break;
			}
		}
		
		//오늘날짜와 계약만료일을 비교해서 계약만료 버튼 활성화를 위해서
		Date date = new Date();
		req.setAttribute("date", date);
		
		req.getRequestDispatcher("/intranet/index.jsp?inc_content=./contract/contract_view.jsp").forward(req, resp);
	}
	
	
}
